home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / unix / unz50p1.zoo / file_io.c < prev    next >
C/C++ Source or Header  |  1993-01-11  |  34KB  |  1,166 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   file_io.c
  4.  
  5.   This file contains routines for doing direct input/output, file-related
  6.   sorts of things.  Most of the system-specific code for unzip is contained
  7.   here, including the non-echoing password code for decryption (bottom).
  8.  
  9.   ---------------------------------------------------------------------------*/
  10.  
  11.  
  12. #if (!defined(__GO32__) && !defined(NeXT))
  13. #  define const
  14. #endif
  15.  
  16. #define FILE_IO_C
  17. #include "unzip.h"
  18.  
  19. #ifdef  MSWIN
  20. #  include "wizunzip.h"
  21. #endif
  22.  
  23.  
  24. /************************************/
  25. /*  File_IO Local Prototypes, etc.  */
  26. /************************************/
  27.  
  28. #if (!defined(DOS_OS2) || defined(MSWIN))
  29.    static int dos2unix __((unsigned char *buf, int len));
  30.    int CR_flag = 0;      /* when last char of buffer == CR (for dos2unix()) */
  31. #endif
  32.  
  33. #ifdef OS2
  34.    extern int   longname;          /* set in mapname.c */
  35.    extern char  longfilename[];
  36. #endif
  37.  
  38. #ifdef CRYPT
  39. #  if (defined(DOS_OS2) || defined(VMS))
  40. #    define MSVMS
  41. #    ifdef DOS_OS2
  42. #      ifdef __EMX__
  43. #        define getch() _read_kbd(0, 1, 0)
  44. #      else
  45. #        ifdef __GO32__
  46. #          include <pc.h>
  47. #          define getch() getkey()
  48. #        else /* !__GO32__ */
  49. #          include <conio.h>
  50. #        endif /* ?__GO32__ */
  51. #      endif
  52. #    else /* !DOS_OS2 */
  53. #      define getch() getc(stderr)
  54. #      define OFF 0   /* for echo control */
  55. #      define ON 1
  56. #      define echoff(f) echo(OFF)
  57. #      define echon()   echo(ON)
  58. #      include <descrip.h>
  59. #      include <iodef.h>
  60. #      include <ttdef.h>
  61. #      if !defined(SS$_NORMAL)
  62. #        define SS$_NORMAL 1   /* only thing we need from <ssdef.h> */
  63. #      endif
  64. #    endif /* ?DOS_OS2 */
  65. #  else /* !(DOS_OS2 || VMS) */
  66. #    ifdef TERMIO       /* Amdahl, Cray, all SysV? */
  67. #      ifdef CONVEX
  68. #        include <sys/termios.h>
  69. #        include <sgtty.h>
  70. #      else /* !CONVEX */
  71. #        ifdef LINUX
  72. #          include <termios.h>
  73. #        else /* !LINUX */
  74. #          include <sys/termio.h>
  75. #        endif /* ?LINUX */
  76. #        define sgttyb termio
  77. #        define sg_flags c_lflag
  78. #      endif /* ?CONVEX */
  79.        int ioctl OF((int, int, voidp *));
  80. #      define GTTY(f,s) ioctl(f,TCGETA,(voidp *)s)
  81. #      define STTY(f,s) ioctl(f,TCSETAW,(voidp *)s)
  82. #    else /* !TERMIO */
  83. #      if (!defined(MINIX) && !defined(__386BSD__))
  84. #        include <sys/ioctl.h>
  85. #      endif /* !MINIX && !__386BSD__ */
  86. #      include <sgtty.h>
  87. #      ifdef __386BSD__
  88. #        define GTTY(f, s) ioctl(f, TIOCGETP, (voidp *) s)
  89. #        define STTY(f, s) ioctl(f, TIOCSETP, (voidp *) s)
  90. #      else /* !__386BSD__ */
  91. #        define GTTY gtty
  92. #        define STTY stty
  93.          int gtty OF((int, struct sgttyb *));
  94.          int stty OF((int, struct sgttyb *));
  95. #      endif /* ?__386BSD__ */
  96. #    endif /* ?TERMIO */
  97.      int isatty OF((int));
  98.      char *ttyname OF((int));
  99. #    if (defined(PROTO) && !defined(__GNUC__) && !defined(_AIX))
  100.        int open (char *, int, ...);
  101. #    endif
  102.      int close OF((int));
  103.      int read OF((int, voidp *, int));
  104. #  endif /* ?(DOS_OS2 || VMS) */
  105. #endif /* CRYPT */
  106.  
  107.  
  108.  
  109.  
  110.  
  111. /******************************/
  112. /* Function open_input_file() */
  113. /******************************/
  114.  
  115. int open_input_file()    /* return non-zero if open failed */
  116. {
  117.     /*
  118.      *  open the zipfile for reading and in BINARY mode to prevent cr/lf
  119.      *  translation, which would corrupt the bitstreams
  120.      */
  121.  
  122. #ifdef VMS
  123.     zipfd = open(zipfn, O_RDONLY, 0, "ctx=stm");
  124. #else /* !VMS */
  125. #ifdef UNIX
  126.     zipfd = open(zipfn, O_RDONLY);
  127. #else /* !UNIX */
  128. #ifdef MACOS
  129.     zipfd = open(zipfn, 0);
  130. #else /* !MACOS */
  131.     zipfd = open(zipfn, O_RDONLY | O_BINARY);
  132. #endif /* ?MACOS */
  133. #endif /* ?UNIX */
  134. #endif /* ?VMS */
  135.     if (zipfd < 1) {
  136.         fprintf(stderr, "error:  can't open zipfile [ %s ]\n", zipfn);
  137.         return (1);
  138.     }
  139.     return 0;
  140. }
  141.  
  142.  
  143.  
  144.  
  145.  
  146. /**********************/
  147. /* Function readbuf() */
  148. /**********************/
  149.  
  150. int readbuf(buf, size)
  151.     char *buf;
  152.     register unsigned size;
  153. {                               /* return number of bytes read into buf */
  154.     register int count;
  155.     int n;
  156.  
  157.     n = size;
  158.     while (size) {
  159.         if (incnt == 0) {
  160.             if ((incnt = read(zipfd, (char *)inbuf, INBUFSIZ)) <= 0)
  161.                 return (n-size);
  162.             /* buffer ALWAYS starts on a block boundary:  */
  163.             cur_zipfile_bufstart += INBUFSIZ;
  164.             inptr = inbuf;
  165.         }
  166.         count = MIN(size, (unsigned)incnt);
  167.         memcpy(buf, inptr, count);
  168.         buf += count;
  169.         inptr += count;
  170.         incnt -= count;
  171.         size -= count;
  172.     }
  173.     return (n);
  174. }
  175.  
  176.  
  177.  
  178.  
  179.  
  180. #ifndef VMS   /* for VMS use code in vms.c (old VMS code below is retained
  181.                * in case of problems...will be removed in a later release) */
  182.  
  183. /*********************************/
  184. /* Function create_output_file() */
  185. /*********************************/
  186.  
  187. int create_output_file()         /* return non-0 if creat failed */
  188. {
  189. /*---------------------------------------------------------------------------
  190.     Create the output file with appropriate permissions.  If we've gotten to
  191.     this point and the file still exists, we have permission to blow it away.
  192.   ---------------------------------------------------------------------------*/
  193.  
  194. #if (!defined(DOS_OS2) || defined(MSWIN))
  195.     CR_flag = 0;   /* hack to get CR at end of buffer working */
  196. #endif
  197.  
  198. #if (defined(UNIX) && !defined(AMIGA))
  199.     {
  200.         int mask;
  201.  
  202. #ifndef VMS
  203.         if (!stat(filename, &statbuf) && (unlink(filename) < 0)) {
  204.             fprintf(stderr, "\n%s:  cannot delete old copy\n", filename);
  205.             return 1;
  206.         }
  207. #       define EXTRA_ARGS
  208. #else /* VMS */
  209. #       define EXTRA_ARGS   ,"rfm=stmlf","rat=cr"
  210. #endif /* ?VMS */
  211.  
  212.         mask = umask(0);   /* now know that we own it */
  213.         outfd = creat(filename, 0xffff & pInfo->unix_attr  EXTRA_ARGS);
  214.         umask(mask);                                            /* VMS, Unix */
  215.     }
  216. #else /* !UNIX || AMIGA */  /* file permissions set after file closed */
  217. #ifndef MACOS
  218.     outfd = creat(filename, S_IWRITE | S_IREAD);     /* DOS, OS2, Mac, Amiga */
  219. #else /* MACOS */
  220.     {
  221.         short fDataFork=TRUE;
  222.         MACINFO mi;
  223.         OSErr err;
  224.  
  225.         fMacZipped = FALSE;
  226.         CtoPstr(filename);
  227.         if (extra_field &&
  228.             (lrec.extra_field_length > sizeof(MACINFOMIN)) &&
  229.             (lrec.extra_field_length <= sizeof(MACINFO))) {
  230.             BlockMove(extra_field, &mi, lrec.extra_field_length);
  231.             if ((makeword((byte *)&mi.header) == 1992) &&
  232.                 (makeword((byte *)&mi.data) ==
  233.                   lrec.extra_field_length-sizeof(ZIP_EXTRA_HEADER)) &&
  234.                 (mi.signature == 'JLEE')) {
  235.                 gostCreator = mi.finfo.fdCreator;
  236.                 gostType = mi.finfo.fdType;
  237.                 fDataFork = (mi.flags & 1) ? TRUE : FALSE;
  238.                 fMacZipped = true;
  239.                 /* If it was Zipped w/Mac version, the filename has either */
  240.                 /* a 'd' or 'r' appended.  Remove the d/r when unzipping */
  241.                 filename[0]-=1;
  242.             }
  243.         }
  244.         if (!fMacZipped) {
  245.             if (!aflag)
  246.                 gostType = gostCreator = '\?\?\?\?';
  247.             else {
  248. #ifdef THINK_C
  249.                 gostCreator = 'KAHL';
  250. #else
  251. #ifdef MCH_MACINTOSH
  252.                 gostCreator = 'Manx';
  253. #else
  254.                 gostCreator = 'MPS ';
  255. #endif
  256. #endif
  257.                 gostType = 'TEXT';
  258.             }
  259.         }
  260.         PtoCstr(filename);
  261.  
  262.         outfd = creat(filename, 0);
  263.         if (fMacZipped) {
  264.             CtoPstr(filename);
  265.             if (hfsflag) {
  266.                 HParamBlockRec   hpbr;
  267.     
  268.                 hpbr.fileParam.ioNamePtr = (StringPtr)filename;
  269.                 hpbr.fileParam.ioVRefNum = gnVRefNum;
  270.                 hpbr.fileParam.ioDirID = glDirID;
  271.                 hpbr.fileParam.ioFlFndrInfo = mi.finfo;
  272.                 hpbr.fileParam.ioFlCrDat = mi.lCrDat;
  273.                 hpbr.fileParam.ioFlMdDat = mi.lMdDat;
  274.                 err = PBHSetFInfo(&hpbr, 0);
  275.             } else {
  276.                 err = SetFInfo((StringPtr)filename , 0, &mi.finfo);
  277.             }
  278.             PtoCstr(filename);
  279.